package csu.edu.flatprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import csu.edu.flatprogram.common.CommonResponse;
import csu.edu.flatprogram.entity.Account;
import csu.edu.flatprogram.entity.DonateGoods;
import csu.edu.flatprogram.entity.RaiseFund;
import csu.edu.flatprogram.persistence.AccountMapper;
import csu.edu.flatprogram.persistence.DonateGoodsMapper;
import csu.edu.flatprogram.persistence.RaiseFundMapper;
import csu.edu.flatprogram.service.RaiseFundService;
import csu.edu.flatprogram.vo.DonateGoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("raiseFundService")
public class RaiseFundServiceImpl implements RaiseFundService {

    @Autowired
    private RaiseFundMapper raiseFundMapper;

    @Autowired
    private DonateGoodsMapper donateGoodsMapper;

    @Autowired
    private AccountMapper accountMapper;

    //查询所有募捐信息
    @Override
    public CommonResponse<List<RaiseFund>> getAllRaiseFundInformations() {
        List<RaiseFund> raiseFundList = raiseFundMapper.selectList(null);
        if(raiseFundList.isEmpty()){
            return CommonResponse.createForSuccessMessage("没有RaiseFund信息");
        }
        return CommonResponse.createForSuccess(raiseFundList);
    }

    //根据ID查询单个募捐信息
    @Override
    public CommonResponse<RaiseFund> getRaiseFundById(String fundid) {
        RaiseFund raiseFund = raiseFundMapper.selectById(fundid);
        if(raiseFund == null){
            return  CommonResponse.createForSuccessMessage("没有该ID的募捐信息");
        }
        return CommonResponse.createForSuccess(raiseFund);
    }

    //新增一条募捐信息
    @Override
    public CommonResponse<RaiseFund> addRaiseFund(String fundid, String fundname, String funddetails) {
        RaiseFund raiseFund = new RaiseFund();
        raiseFund.setFundid(fundid);
        raiseFund.setFundname(fundname);
        raiseFund.setFunddetails(funddetails);

        int i = raiseFundMapper.insert(raiseFund);
        return CommonResponse.createForSuccess("已新增了"+fundname+"募捐信息",raiseFund);
    }

    //根据Id更新一条募捐信息
    @Override
    public CommonResponse<RaiseFund> updateRaiseFund(String fundid, String fundname, String funddetails) {
        RaiseFund raiseFund = raiseFundMapper.selectById(fundid);
        raiseFund.setFundid(fundid);
        raiseFund.setFundname(fundname);
        raiseFund.setFunddetails(funddetails);

        int i = raiseFundMapper.updateById(raiseFund);
        return CommonResponse.createForSuccess("已更新了"+fundname+"募捐信息",raiseFund);
    }

    //添加募捐信息post
    @Override
    public CommonResponse<RaiseFund> insertRaisefund(RaiseFund raiseFund) {
//        int i = raiseFundMapper.selectCount(Wrappers.<Account>query().eq("fundid",raiseFund.getFundid()));
//        if(i>0){
//            return CommonResponse.createForError("用户名已存在");
//        }
       // RaiseFund raiseFund1 = raiseFundMapper.selectById(raiseFund.getFundid());
//        if(raiseFund1 != null){
//            return  CommonResponse.createForSuccessMessage("募捐ID已存在");
//        }
//        String fundid = raiseFund.getFundid();
//        if(fundid == null){
//            return CommonResponse.createForError("募捐ID已存在");
//        }

        RaiseFund raiseFund1 = new RaiseFund();
        raiseFund1.setFundid(raiseFund.getFundid());
        raiseFund1.setFundname(raiseFund.getFundname());
        raiseFund1.setFunddetails(raiseFund.getFunddetails());
        raiseFund1.setFundpicture(raiseFund.getFundpicture());

        int i = raiseFundMapper.insert(raiseFund1);
        if(i == 0){
            return CommonResponse.createForSuccessMessage("添加失败");
        }
        return CommonResponse.createForSuccess("添加成功",raiseFund1);

    }

    //更新募捐信息post
    @Override
    public CommonResponse<RaiseFund> updateRaisefund(RaiseFund raiseFund) {
//        RaiseFund raiseFund1 = raiseFundMapper.selectById(raiseFund.getFundid());
//        if(raiseFund1 == null){
//            return  CommonResponse.createForSuccessMessage("该募捐信息不存在");
//        }
//        String fundid = raiseFund.getFundid();
//        if(fundid == null){
//            return CommonResponse.createForError("募捐ID不存在");
//        }

        RaiseFund raiseFund1 = new RaiseFund();
        raiseFund1.setFundid(raiseFund.getFundid());
        raiseFund1.setFundname(raiseFund.getFundname());
        raiseFund1.setFunddetails(raiseFund.getFunddetails());
        raiseFund1.setFundpicture(raiseFund.getFundpicture());

        int i = raiseFundMapper.update(raiseFund1,Wrappers.<RaiseFund>query().eq("fundid",raiseFund.getFundid()));
        if(i == 0){
            return CommonResponse.createForSuccessMessage("更新失败");
        }
        return CommonResponse.createForSuccess("更新成功",raiseFund1);
    }

    //根据Id删除一条募捐信息
    @Override
    public CommonResponse<RaiseFund> deleteRaiseFund(String fundid) {
        int i = raiseFundMapper.deleteById(fundid);
        return CommonResponse.createForSuccessMessage("删除成功");
    }

    //根据ID查询单个募捐信息下所有募捐物品
    @Override
    public CommonResponse<List<DonateGoods>> getDonateGoodsByRaiseFundId(String fundid) {
        QueryWrapper<DonateGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fundid",fundid);

        List<DonateGoods> donateGoodsList = donateGoodsMapper.selectList(queryWrapper);
        if(donateGoodsList.isEmpty()){
            return CommonResponse.createForSuccessMessage("该募捐信息下暂时没有募捐物品");
        }

        return CommonResponse.createForSuccess(donateGoodsList);
    }

    //根据ID查询单个募捐信息下所有募捐物品（包括用户信息）
    @Override
    public CommonResponse<List<DonateGoodsVO>> getDonateGoodsVOByRaiseFundId(String fundid) {
        QueryWrapper<DonateGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fundid",fundid);
        List<DonateGoods> donateGoodsList = donateGoodsMapper.selectList(queryWrapper);
        if(donateGoodsList.isEmpty()){
            return CommonResponse.createForSuccessMessage("该募捐信息下暂时没有募捐物品");
        }

        List<DonateGoodsVO> donateGoodsVOList = new ArrayList<>();
        for(DonateGoods donateGoods:donateGoodsList){
            DonateGoodsVO donateGoodsVO = donategoodsToDonategoodsVo(donateGoods);
            donateGoodsVOList.add(donateGoodsVO);
        }
        return CommonResponse.createForSuccess(donateGoodsVOList);
    }

    //募捐物品信息与用户信息联查展示
    public DonateGoodsVO donategoodsToDonategoodsVo(DonateGoods donateGoods){
        DonateGoodsVO donateGoodsVO = new DonateGoodsVO();
        //DonateGoods信息
        donateGoodsVO.setGoodsid(donateGoods.getGoodsid());
        donateGoodsVO.setUserid(donateGoods.getUserid());
        donateGoodsVO.setFundid(donateGoods.getFundid());
        donateGoodsVO.setGoodsplatform(donateGoods.getGoodsplatform());
        donateGoodsVO.setGoodscategory(donateGoods.getGoodscategory());
        donateGoodsVO.setGoodsnumber(donateGoods.getGoodsnumber());
        donateGoodsVO.setGoodsdescription(donateGoods.getGoodsdescription());

        //Account信息
        Account account = accountMapper.selectById(donateGoodsVO.getUserid());
        donateGoodsVO.setUsername(account.getUsername());
        donateGoodsVO.setPhone(account.getPhone());
        donateGoodsVO.setAddress(account.getAddress());

        //RaiseFund信息
        RaiseFund raiseFund = raiseFundMapper.selectById(donateGoodsVO.getFundid());
        donateGoodsVO.setFundname(raiseFund.getFundname());
        donateGoodsVO.setFunddetails(raiseFund.getFunddetails());

        return donateGoodsVO;
    }

    //查询所有捐赠物品
    @Override
    public CommonResponse<List<DonateGoodsVO>> getDonateGoods() {
        List<DonateGoods> donateGoodsList = donateGoodsMapper.selectList(null);
        if(donateGoodsList.isEmpty()){
            return CommonResponse.createForSuccessMessage("该募捐信息下暂时没有募捐物品");
        }

        List<DonateGoodsVO> donateGoodsVOList = new ArrayList<>();
        for(DonateGoods donateGoods:donateGoodsList){
            DonateGoodsVO donateGoodsVO = donategoodsToDonategoodsVo(donateGoods);
            donateGoodsVOList.add(donateGoodsVO);
        }
        return CommonResponse.createForSuccess(donateGoodsVOList);
    }

    //根据Id查询单个募捐物品
    @Override
    public CommonResponse<DonateGoodsVO> getDonateGoodById(int goodsid) {
        DonateGoods donateGoods = donateGoodsMapper.selectById(goodsid);
        if(donateGoods == null){
            return CommonResponse.createForSuccessMessage("没有该ID的剧增物品");
        }
        DonateGoodsVO donateGoodsVO = donategoodsToDonategoodsVo(donateGoods);
        return CommonResponse.createForSuccess(donateGoodsVO);
    }

    //新增一条捐赠物品信息
    @Override
    public CommonResponse<DonateGoods> addDonateGood(String userid, String fundid, String goodsplatform, String goodscategory, String goodsnumber, String goodsdescription) {
        DonateGoods donateGoods = new DonateGoods();
        donateGoods.setUserid(userid);
        donateGoods.setFundid(fundid);
        donateGoods.setGoodsplatform(goodsplatform);
        donateGoods.setGoodscategory(goodscategory);
        donateGoods.setGoodsnumber(goodsnumber);
        donateGoods.setGoodsdescription(goodsdescription);

        int i = donateGoodsMapper.insert(donateGoods);
        return CommonResponse.createForSuccess("已新增了"+fundid+"下的捐赠物品信息",donateGoods);
    }

    //根据ID更新一条捐赠物品信息
    @Override
    public CommonResponse<DonateGoods> updateDonateGood(int goodsid,String userid, String fundid, String goodsplatform, String goodscategory, String goodsnumber, String goodsdescription) {
        DonateGoods donateGoods = donateGoodsMapper.selectById(goodsid);
        donateGoods.setUserid(userid);
        donateGoods.setFundid(fundid);
        donateGoods.setGoodsplatform(goodsplatform);
        donateGoods.setGoodscategory(goodscategory);
        donateGoods.setGoodsnumber(goodsnumber);
        donateGoods.setGoodsdescription(goodsdescription);

        int i = donateGoodsMapper.updateById(donateGoods);
        return CommonResponse.createForSuccess("已更新了"+fundid+"下"+goodsid+"的捐赠物品信息",donateGoods);
    }

    //新增一条捐赠物品信息post
    @Override
    public CommonResponse<DonateGoods> insertDonategoods(DonateGoods donateGoods) {
        DonateGoods donateGoods1 = new DonateGoods();

        donateGoods1.setUserid(donateGoods.getUserid());
        donateGoods1.setFundid(donateGoods.getFundid());
        donateGoods1.setGoodsplatform(donateGoods.getGoodsplatform());
        donateGoods1.setGoodscategory(donateGoods.getGoodscategory());
        donateGoods1.setGoodsnumber(donateGoods.getGoodsnumber());
        donateGoods1.setGoodsdescription(donateGoods.getGoodsdescription());

        int i = donateGoodsMapper.insert(donateGoods1);
        if(i == 0){
            return CommonResponse.createForSuccessMessage("添加失败");
        }
        return CommonResponse.createForSuccess("添加成功",donateGoods1);
    }

    //更新一条捐赠物品信息post
    @Override
    public CommonResponse<DonateGoods> updateDonategoods(DonateGoods donateGoods) {
        int i = donateGoodsMapper.selectCount(Wrappers.<DonateGoods>query().eq("goodsid",donateGoods.getGoodsid()));
        if(i == 0){
            return CommonResponse.createForError("捐赠物品ID不存在");
        }
        DonateGoods donateGoods1 = new DonateGoods();

        donateGoods1.setGoodsid(donateGoods.getGoodsid());
        donateGoods1.setUserid(donateGoods.getUserid());
        donateGoods1.setFundid(donateGoods.getFundid());
        donateGoods1.setGoodsplatform(donateGoods.getGoodsplatform());
        donateGoods1.setGoodscategory(donateGoods.getGoodscategory());
        donateGoods1.setGoodsnumber(donateGoods.getGoodsnumber());
        donateGoods1.setGoodsdescription(donateGoods.getGoodsdescription());

        i = donateGoodsMapper.update(donateGoods1,Wrappers.<DonateGoods>query().eq("goodsid",donateGoods.getGoodsid()));
        if(i == 0){
            return CommonResponse.createForSuccessMessage("更新失败");
        }
        return CommonResponse.createForSuccess("更新成功",donateGoods1);
    }

    //根据ID删除捐赠物品信息
    @Override
    public CommonResponse<DonateGoods> deleteDonateGoodById(int goodsdid) {
        int i = donateGoodsMapper.deleteById(goodsdid);
        return CommonResponse.createForSuccessMessage("删除成功");
    }
}
